import time
import os
import cv2
from PIL import Image
from core.util import time_str
# from core.image_util import ssim_compare, match_flag
import setting as c
from core.log import logger

#### 是否在战斗
def is_fight(img_hwnd, hwnd):
    
    opencv_image = cv2.imread(os.path.join(c.shot_dir_path, r'g1.jpg'))
    if opencv_image is None:
        return None, False
    rgb_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
    img_hwnd = Image.fromarray(rgb_image)
    
    img_crop = img_hwnd.crop(c.flag_tag_fighting_cropbox)
    ts = time_str()
    file_img = f"{c.shot_dir_path}/auto_fight_{hwnd}_{ts}.png"
    img_crop.save(file_img, 'PNG')
    
    fighting = False
    rate = 0
    for flag_fighting in c.flag_tag_fighting:
        rate = ssim_compare(flag_fighting, file_img)
        if rate > 0.95:
            fighting = True
            break
    logger.info(f'fighting {hwnd} {rate:.2f} {fighting}')
    
    return fighting, True


def find_mouse(img_path):
    
    img_path = os.path.join(c.shot_dir_path, r'g1.jpg')
    
    return match_flag(img_path, c.flag_tag_mouse)


def is_popup(file_img_1, file_img_2):
    rate = ssim_compare(file_img_1, file_img_2)
    if rate > 0.95:
        return True
    else:
        return False

def test_find_mouse(hwnd):
    while True:
        time.sleep(c.AUTO_INTERVAL)
        